package com.momo.mal.util;

import net.momo.mal.R;
import com.momo.mal.anime.AnimeResult;
import com.momo.mal.anime.overall.AnimeOverall;
import com.momo.mal.manga.MangaResult;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockDialogFragment;

public class DialogSearch extends SherlockDialogFragment {
	private String searchBy = "";
	
    public DialogSearch newInstance(String searchBy) {
    	DialogSearch dialog = new DialogSearch();
    	dialog.searchBy = searchBy;
    	
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		final View viewInflate = inflater.inflate(R.layout.dialog_search, null);
		
		if(searchBy.equalsIgnoreCase("anime")) {
			builder.setTitle(getResources().getString(R.string.hint_anime));
		} 
		if(searchBy.equalsIgnoreCase("manga")){
			builder.setTitle(getResources().getString(R.string.hint_manga));
		}
//		if(searchBy.equalsIgnoreCase("friend")){
//			builder.setTitle(getResources().getString(R.string.hint_friend));
//		}
		
		builder.setView(viewInflate)
				.setPositiveButton("Search",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								Intent intent = null;
								EditText editSearch = (EditText) viewInflate
										.findViewById(R.id.edit_search);
								
								if(searchBy.equalsIgnoreCase("anime")) {
									intent = new Intent(getActivity(), AnimeResult.class);
									intent.putExtra("query", editSearch.getText().toString().replace(" ", "%20"));
								} 
								if(searchBy.equalsIgnoreCase("manga")){
									intent = new Intent(getActivity(), MangaResult.class);
									intent.putExtra("query", editSearch.getText().toString().replace(" ", "%20"));
								}
//								if(searchBy.equalsIgnoreCase("friend")) {
//									intent = new Intent(getActivity(), AnimeOverall.class);
//									intent.putExtra("username", editSearch.getText().toString().replace(" ", "%20"));
//								}
								
								if(intent != null) {
									startActivity(intent);
								}
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		return builder.create();
    }
}
